﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;
using System.Collections;

namespace WXYW
{
    public partial class BJXXAllOnProcess : System.Web.UI.Page
    {
        public int RecordsNumber { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindForms();
            }
        }

        private void BindForms()
        {
            ApplicationProductService service = new ApplicationProductService();
            List<ApplicationProduct> products = service.GetAllBJXXProduct(keyWordTextBox.Text);
            RecordsNumber = products.Count;
            BJXXFormsGridView.DataSource = products;
            BJXXFormsGridView.DataBind();
        }

        //protected void viewLinkButton_click(object sender, EventArgs e)
        //{
        //    string productID = ((LinkButton)sender).CommandArgument;
        //    Response.Redirect(string.Format("BJXXDisagreeForm.aspx?{0}={1}", Constants.QUERYSTR_PRODUCT_ID, productID));
        //}

        protected void BJXXFormsGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (e.NewPageIndex >= 0 && e.NewPageIndex < BJXXFormsGridView.PageCount)
            {
                BJXXFormsGridView.PageIndex = e.NewPageIndex;
            }
            BindForms();

        }

        protected void BJXXFormsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
                HyperLink spcodeLink = e.Row.FindControl("spCodeHyperLink") as HyperLink;
                ApplicationProduct product = e.Row.DataItem as ApplicationProduct;
                if (product.BJXXIsReal.HasValue)
                {
                    ((Label)e.Row.FindControl("isRealLabel")).Text = product.BJXXIsReal.Value ? "是" : "否";
                }
                if (!string.IsNullOrEmpty(product.SPCode))
                {
                    spcodeLink.Text = product.SPCode;
                    spcodeLink.NavigateUrl = string.Format("BJXXApplicationDetail.aspx?{0}={1}", Constants.QUERYSTR_PRODUCT_ID, product.ApplicationProductID);
                }
            }

            if (e.Row.RowType == DataControlRowType.Pager)
            {
                DropDownList pageDDL = e.Row.FindControl("pageDDL") as DropDownList;
                for (int index = 1; index <= BJXXFormsGridView.PageCount; index++)
                {
                    pageDDL.Items.Add(index.ToString());
                }
                pageDDL.SelectedValue = (BJXXFormsGridView.PageIndex + 1).ToString();
            }
        }

        protected IEnumerable GridView_MustAddARow(IEnumerable data)
        {
            List<ApplicationProduct> dds = new List<ApplicationProduct>();
            dds.Add(new ApplicationProduct());
            return dds;
        }

        protected void pageDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList pageDDL = sender as DropDownList;
            BJXXFormsGridView.PageIndex = Convert.ToInt32(pageDDL.SelectedIndex);
            BindForms();
        }

        protected void BJXXFormsGridView_DataBound(object sender, EventArgs e)
        {
            if (BJXXFormsGridView.BottomPagerRow != null)
            {
                BJXXFormsGridView.BottomPagerRow.Visible = true;
            }
        }

        protected void searchLinkButton_Click(object sender, EventArgs e)
        {
            BindForms();
        }

        protected void resetLinkButton_Click(object sender, EventArgs e)
        {
            keyWordTextBox.Text = "";
            BindForms();
        }

        protected void BJXXFormsGridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            ApplicationProductService service = new ApplicationProductService();
            List<ApplicationProduct> products = service.GetAllBJXXProduct(keyWordTextBox.Text);
            RecordsNumber = products.Count;

            string sortDirection = "asc";
            if (Session["OrderDirection"] != null)
            {
                sortDirection = Session["OrderDirection"].ToString();
                if (sortDirection.Equals("asc"))
                {
                    Session["OrderDirection"] = "desc";
                }
                else
                {
                    Session["OrderDirection"] = "asc";
                }
            }
            else
            {
                Session["OrderDirection"] = "asc";
            }

            BJXXFormsGridView.DataSource = products.AsQueryable().OrderBy(string.Format("{0} {1}", e.SortExpression, sortDirection)).ToList();
            BJXXFormsGridView.DataBind();
        }
    }
}
